package com.phoenix.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.phoenix.entity.Events;
import com.phoenix.entity.EventsSigns;
import com.phoenix.entity.ResponseData;
import com.phoenix.entity.Users;
import com.phoenix.service.EventsService;
import com.phoenix.util.MarkdownUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiresRoles("admin")
@RequestMapping("/events")
public class EventsController {
    @Autowired
    EventsService eventsService;

    @GetMapping("/addEventsPage")
    public String addEventsPage(Model model) {
        Events events = new Events();
        model.addAttribute("method", "post");
        model.addAttribute("events", events);
        return "events/eventsAdd";
    }

    @GetMapping("/eventsEdit/{eventsID}")
    public String eventsEdit(Model model, @PathVariable Integer eventsID) {
        Events events = eventsService.selectEventsByID(eventsID);
        model.addAttribute("events", events);
        model.addAttribute("method", "put");
        return "events/eventsAdd";
    }

    @GetMapping("/eventsList")
    public String eventsList(Model model, @RequestParam(value = "keywords", defaultValue = "") String keywords, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        //进行分页，提供关键字查询
        //pageSize为分页的行数
        Map<String, Object> search = new HashMap<>();
        search.put("keywords", keywords);
        PageHelper.startPage(pageNum, pageSize);
        List<Events> list = eventsService.selectNSearch(search);
        //使用PageHelper包装数据
        PageInfo<Events> pageInfo = new PageInfo<>(list);
        model.addAttribute("result", new ResponseData<>(200, "ok", pageInfo));
        model.addAttribute("keywords", keywords);
        return "events/eventsList";
    }

    @DeleteMapping("/deleteEvents")
    @ResponseBody
    public ResponseData<String> deleteEvents(Model model, @RequestParam Integer eventsID) {
        if (eventsService.deleteEventsByID(eventsID)) {
            return new ResponseData<>(200, "删除成功！", null);
        } else {
            return new ResponseData<>(500, "删除失败！", null);
        }
    }

    @PostMapping("/events")
    public String addEvents(Model model, Events events) {
        model.addAttribute("events", events);
        if (eventsService.addEvents(events)) {
            if (events.getStatus() == 0) {
                model.addAttribute("message", "活动保存成功!");
            } else if (events.getStatus() == 1) {
                model.addAttribute("message", "活动发布成功!");
            }
        } else {
            if (events.getStatus() == 0) {
                model.addAttribute("message", "活动保存失败!");
            } else if (events.getStatus() == 1) {
                model.addAttribute("message", "活动发布失败!");
            }
        }
        return "events/eventsAdd";
    }

    @PutMapping("/events")
    public String update(Model model, Events events) throws InterruptedException {
        model.addAttribute("events", events);
        if (eventsService.updateEvents(events)) {
            model.addAttribute("message", "活动更新成功!");
        } else {
            model.addAttribute("message", "活动更新失败!");
            wait(1000);
            return "events/eventsList";
        }
        return "events/eventsAdd";
    }
}
